<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运行GLib应用程序</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="GLib 参考手册">
<link rel="up" href="glib.html" title="GLib概述">
<link rel="prev" href="glib-compiling.html" title="编译GLib应用程序">
<link rel="next" href="glib-changes.html" title="GLib 的更改">
<meta name="generator" content="GTK-Doc V1.18 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="glib-compiling.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="glib.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GLib 参考手册</th>
<td><a accesskey="n" href="glib-changes.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="glib-running"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">运行GLib应用程序</span></h2>
<p>运行GLib应用程序 — 如何运行和调试GLib应用程序</p>
</td>
<td valign="top" align="right"></td>
</tr></table></div>
<div class="refsect1">
<a name="idp3381056"></a><h2>Running and debugging GLib Applications</h2>
<div class="refsect2">
<a name="idp5441320"></a><h3>环境变量</h3>
<p>
  The runtime behaviour of GLib applications can be influenced by a
  number of environment variables.
</p>
<p><b>Standard variables. </b>
    GLib reads standard environment variables like <code class="envar">LANG</code>,
    <code class="envar">PATH</code>, <code class="envar">HOME</code>, <code class="envar">TMPDIR</code>,
    <code class="envar">TZ</code> and <code class="envar">LOGNAME</code>.
  </p>
<p><b>XDG 目录. </b>
    GLib consults the environment variables <code class="envar">XDG_DATA_HOME</code>,
    <code class="envar">XDG_DATA_DIRS</code>, <code class="envar">XDG_CONFIG_HOME</code>,
    <code class="envar">XDG_CONFIG_DIRS</code>, <code class="envar">XDG_CACHE_HOME</code> and
    <code class="envar">XDG_RUNTIME_DIR</code> for the various XDG directories.
    For more information, see the <a class="ulink" href="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html" target="_top">XDG basedir spec</a>.
  </p>
<p><a name="G_FILENAME_ENCODING"></a><b><code class="envar">G_FILENAME_ENCODING</code>. </b>这个环境变量可以被设置为逗号为分隔字符集名称的列表. GLib 假设文件名编码是以此列表第一个变量指定的编码方式而不是UTF-8. 特别标记 "@locale" 可用于指定当前语言环境的字符集编码.</p>
<p><a name="G_BROKEN_FILENAMES"></a><b><code class="envar">G_BROKEN_FILENAMES</code>. </b>如果设置了这个环境变量,GLib 假设文件名为当前语言环境编码而不是UTF-8. G_FILENAME_ENCODING 优先于 G_BROKEN_FILENAMES.</p>
<p><a name="G_MESSAGES_PREFIXED"></a><b><code class="envar">G_MESSAGES_PREFIXED</code>. </b>
    A list of log levels for which messages should be prefixed by the
    program name and PID of the application. The default is to prefix
    everything except <code class="literal">G_LOG_LEVEL_MESSAGE</code> and
    <code class="literal">G_LOG_LEVEL_INFO</code>.
    The possible values are
    <code class="literal">error</code>,
    <code class="literal">warning</code>,
    <code class="literal">critical</code>,
    <code class="literal">message</code>,
    <code class="literal">info</code> and
    <code class="literal">debug</code>.
    You can also use the special values
    <code class="literal">all</code> and
    <code class="literal">help</code>.
  
    This environment variable only affects the default log handler,
    g_log_default_handler().
  </p>
<p><a name="G_MESSAGES_DEBUG"></a><b><code class="envar">G_MESSAGES_DEBUG</code>. </b>
    A space-separated list of log domains for which informational
    and debug messages should be printed. By default, these
    messages are not printed.
  
    You can also use the special value <code class="literal">all</code>.
  
    This environment variable only affects the default log handler,
    g_log_default_handler().
  </p>
<p><a name="G-DEBUG:CAPS"></a><b><code class="envar">G_DEBUG</code>. </b>
    This environment variable can be set to a list of debug options,
    which cause GLib to print out different types of debugging information.
    </p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term">fatal-warnings</span></p></td>
<td><p>Causes GLib to abort the program at the first call
           to g_warning() or g_critical().</p></td>
</tr>
<tr>
<td><p><span class="term">fatal-criticals</span></p></td>
<td><p>Causes GLib to abort the program at the first call
           to g_critical().</p></td>
</tr>
<tr>
<td><p><span class="term">gc-friendly</span></p></td>
<td><p>Newly allocated memory that isn't directly initialized,
          as well as memory being freed will be reset to 0. The point here is
          to allow memory checkers and similar programs that use Boehm GC alike
          algorithms to produce more accurate results.</p></td>
</tr>
<tr>
<td><p><span class="term">resident-modules</span></p></td>
<td><p>All modules loaded by GModule will be made resident.
          This can be useful for tracking memory leaks in modules which are
          later unloaded; but it can also hide bugs where code is accessed
          after the module would have normally been unloaded.</p></td>
</tr>
<tr>
<td><p><span class="term">bind-now-modules</span></p></td>
<td><p>All modules loaded by GModule will bind their symbols
          at load time, even when the code uses %G_MODULE_BIND_LAZY.</p></td>
</tr>
</tbody>
</table></div>
<p>
    The special value all can be used to turn on all debug options.
    The special value help can be used to print all available options.
  </p>
<p><a name="G_SLICE"></a><b><code class="envar">G_SLICE</code>. </b>该环境变量允许重新配置GSlice内存分配器. </p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term">always-malloc</span></p></td>
<td><p>This will cause all slices allocated through
          g_slice_alloc() and released by g_slice_free1() to be actually
          allocated via direct calls to g_malloc() and g_free().
          This is most useful for memory checkers and similar programs that
          use Boehm GC alike algorithms to produce more accurate results.
          It can also be in conjunction with debugging features of the system's
          malloc() implementation such as glibc's MALLOC_CHECK_=2 to debug
          erroneous slice allocation code, although
          <code class="literal">debug-blocks</code> is usually a better suited debugging
          tool.</p></td>
</tr>
<tr>
<td><p><span class="term">debug-blocks</span></p></td>
<td>
<p>Using this option (present since GLib 2.13) engages
          extra code which performs sanity checks on the released memory
          slices. Invalid slice adresses or slice sizes will be reported and
          lead to a program halt. This option is for debugging scenarios.
          In particular, client packages sporting their own test suite should
          <span class="emphasis"><em>always enable this option when running tests</em></span>.
          Global slice validation is ensured by storing size and address
          information for each allocated chunk, and maintaining a global
          hash table of that data. That way, multi-thread scalability is
          given up, and memory consumption is increased. However, the
          resulting code usually performs acceptably well, possibly better
          than with comparable memory checking carried out using external
          tools.</p>
<p>An example of a memory corruption scenario that cannot be
          reproduced with <code class="literal">G_SLICE=always-malloc</code>, but will
          be caught by <code class="literal">G_SLICE=debug-blocks</code> is as follows:
          </p>
<pre class="programlisting">
            void *slist = g_slist_alloc (); /* void* gives up type-safety */
            g_list_free (slist);            /* corruption: sizeof (GSList) != sizeof (GList) */
          </pre>
</td>
</tr>
</tbody>
</table></div>
<p> 特殊值可以用来打开所有的调试选项. 特殊值可以用来打印所有可用的选项.</p>
<p><a name="G_RANDOM_VERSION"></a><b><code class="envar">G_RANDOM_VERSION</code>. </b>
    If this environment variable is set to '2.0', the outdated
    pseudo-random number seeding and generation algorithms from
    GLib 2.0 are used instead of the newer, better ones. You should
    only set this variable if you have sequences of numbers that were
    generated with Glib 2.0 that you need to reproduce exactly.
  </p>
<p><a name="LIBCHARSET_ALIAS_DIR"></a><b><code class="envar">LIBCHARSET_ALIAS_DIR</code>. </b>　允许为<code class="filename">charset.aliases</code>文件指定一个非标准的位置, 这文件通常用于字符集转换全程.默认位置是编译时指定的<em class="replaceable"><code>libdir</code></em>.</p>
<p><a name="TZDIR"></a><b><code class="envar">TZDIR</code>. </b>
    Allows to specify a nonstandard location for the timezone data files
    that are used by the #GDateTime API. The default location is under
    <code class="filename">/usr/share/zoneinfo</code>. For more information,
    also look at the <span class="command"><strong>tzset</strong></span> manual page.
  </p>
</div>
<hr>
<div class="refsect2">
<a name="setlocale"></a><h3>区域设置</h3>
<p>相当数量的GLib接口取决于应用程序在运行时的当前区域设置. 因此大多数使用GLib的程序应该调用<code class="function">setlocale(LC_ALL,"")</code> 来设置当前区域.</p>
<p>在Windows中,C程序中有几个区域设置的概念并不是同步的. 一方面,有系统默认的ANSI代码页,由C库和Win32API来决定用什么 编码来处理文件名.(我们现在谈论的是"窄"函数处理字符指针,不是 "宽"的那种.)</p>
<p>另一方面,有一个C函数库的当前区域.该字符集(代码页)并不一定像系统默认ANSI代码页一样使用.它返回的字符集类似<code class="function">strftime()</code>.</p>
</div>
<hr>
<div class="refsect2">
<a name="idp3572960"></a><h3>陷阱与追踪</h3>
<p> 在GLib配置好<code class="option">--enable-debugging=yes</code> 的调试中,有些包含陷阱变量的代码可被设置.这些陷阱导致代码停止检查 当前的程序状态并回溯.</p>
<p>当前,有下列陷阱变量存在: </p>
<pre class="programlisting">
static volatile gulong g_trap_free_size;
static volatile gulong g_trap_realloc_size;
static volatile gulong g_trap_malloc_size;
</pre>
<p> 如果设置 size &gt; 0, 其大小匹配内存块的大小的话,<a class="link" href="glib-Memory-Allocation.html#g-free" title="g_free ()">g_free</a>(), <a class="link" href="glib-Memory-Allocation.html#g-realloc" title="g_realloc ()">g_realloc</a>()和 <a class="link" href="glib-Memory-Allocation.html#g-malloc" title="g_malloc ()">g_malloc</a>() 将被拦截. 这只会在<code class="literal">g_mem_set_vtable(glib_mem_profiler_table)</code>启动时才会生效, 因为内存分析需要匹配内存块的大小.</p>
<p>注意,许多现代调试器支持条件断点,其实现大同小异,例如.在gdb中，你可以做 </p>
<pre class="programlisting">
break g_malloc
condition 1 n_bytes == 20
</pre>
<p> 来在只有g_malloc()分配20字节的时候才中断它.</p>
</div>
<hr>
<div class="refsect2">
<a name="idp3577952"></a><h3>Gdb debugging macros</h3>
<p>
glib ships with a set of python macros for the gdb debugger. These includes pretty
printers for lists, hashtables and gobject types. It also has a backtrace filter
that makes backtraces with signal emissions easier to read.
</p>
<p>
To use this you need a recent enough gdb that supports python scripting. Gdb 7.0
should be recent enough, but branches of the "archer" gdb tree as used in Fedora 11
and Fedora 12 should work too. You then need to install glib in the same prefix as
gdb so that the python gdb autoloaded files get installed in the right place for
gdb to pick up.
</p>
<p>
General pretty printing should just happen without having to do anything special.
To get the signal emission filtered backtrace you must use the "new-backtrace" command
instead of the standard one.
</p>
<p>
There is also a new command called gforeach that can be used to apply a command
on each item in a list. E.g. you can do
</p>
<pre class="programlisting">
gforeach i in some_list_variable: print *(GtkWidget *)l
</pre>
<p>
Which would print the contents of each widget in a list of widgets.
</p>
</div>
<hr>
<div class="refsect2">
<a name="idp3580616"></a><h3>SystemTap</h3>
<p>
<a class="ulink" href="http://sourceware.org/systemtap/" target="_top">SystemTap</a> is a dynamic whole-system
analysis toolkit.  GLib ships with a file <code class="filename">glib.stp</code> which defines a
set of probe points, which you can hook into with custom SystemTap scripts.
See the files <code class="filename">glib.stp</code> and <code class="filename">gobject.stp</code> which
are in your shared SystemTap scripts directory.
</p>
</div>
<hr>
<div class="refsect2">
<a name="idp3582920"></a><h3>Memory statistics</h3>
<p>
g_mem_profile() will output a summary g_malloc() memory usage, if memory
profiling has been enabled by calling
<code class="literal">g_mem_set_vtable (glib_mem_profiler_table)</code> upon startup.
</p>
<p>如果GLib已经配置了选项<code class="option">--enable-debug=yes</code>, 那么在调试器中则可以调用g_slice_debug_tree_statistics()来查看 内存片使用的细节.</p>
</div>
</div>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.18</div>
</body>
</html>